
// let duration = Duration::new(5, 730_000_000); // 5秒 + 730毫秒
// from_secs(secs: u64)	从秒创建	Duration::from_secs(5)
// from_millis(millis: u64)	从毫秒创建	Duration::from_millis(500)
// from_micros(micros: u64)	从微秒创建	Duration::from_micros(500_000)
// from_nanos(nanos: u64)	从纳秒创建	Duration::from_nanos(1_000_000_000)
// println!("秒: {}", duration.as_secs());      // 5
// println!("毫秒: {}", duration.as_millis());  // 5730
// println!("微秒: {}", duration.as_micros());  // 5_730_000
// println!("纳秒: {}", duration.as_nanos());   // 5_730_000_000
use chrono::prelude::*;
fn main() {
    {
        let tmptm = std::time::SystemTime::now();
        // std::time::Duration
        if let Ok(duration_since_the_epoch) = tmptm.duration_since(std::time::UNIX_EPOCH) {
            // 将持续时间转换为秒。
            // 不能转为日期。
            let timestamp = duration_since_the_epoch.as_secs();
            println!("Current timestamp: {:?}", duration_since_the_epoch);
            println!("Current timestamp: {:?}", timestamp);
        } else {
            println!("SystemTime before UNIX EPOCH!");
        }
    }
    {
        // 记录程序开始的时间点
        // std::time::Instant,Instant { tv_sec: 879556, tv_nsec: 769100544 }
        // 不能获取tv_sec,tv_nsec
        let start = std::time::Instant::now();
        println!("[{},{}]out:{:?}", file!(), line!(), type_str(&start));
        println!("[{},{}]out:{:?}", file!(), line!(), start);
        // println!("[{},{}]out:{}-{}", file!(), line!(), start.tv_sec(), start.tv_nsec());
        // 模拟一些工作，例如等待一秒
        std::thread::sleep(std::time::Duration::from_secs(1));
        println!("[{},{}]out:{}", file!(), line!(), start.elapsed().as_millis());
        // // 获取结束时间点
        // let end = std::time::Instant::now();
        // // 计算持续时间
        // let duration = end.duration_since(start);
        // println!("Time elapsed in seconds: {:?}", duration.as_secs());
        
    }
    {
        let dt = chrono::NaiveDateTime::parse_from_str("2023-05-20 14:30:15", "%Y-%m-%d %H:%M:%S").unwrap();
        // 获取日期部分
        let date = dt.date();  // NaiveDate
        println!("日期: {}", date);  // 2023-05-20
        // 获取时间部分
        let time = dt.time();  // NaiveTime
        println!("时间: {}", time);  // 14:30:15
        // 获取各字段
        println!("年: {}", dt.year());      // 2023
        println!("月: {}", dt.month());     // 5
        println!("日: {}", dt.day());       // 20
        println!("时: {}", dt.hour());      // 14
        println!("分: {}", dt.minute());    // 30
        println!("秒: {}", dt.second());    // 15
        println!("纳秒: {}", dt.nanosecond()); // 0
        println!("星期: {}", dt.weekday()); // Sat
        // println!("[{},{}]out:{}", file!(), line!(), chk_if_option(None));
        // println!("[{},{}]out:{}", file!(), line!(), chk_if_option(Some(8)));
    }
    {
        let tmptm = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap();
        println!("stamp: {}", tmptm.as_millis());
        let tmpdt = duration_to_naivedatetime(tmptm);
        println!("年: {}", tmpdt.year());      // 2023
        println!("月: {}", tmpdt.month());     // 5
        println!("日: {}", tmpdt.day());       // 20
        println!("时: {}", tmpdt.hour());      // 14
        println!("分: {}", tmpdt.minute());    // 30
        println!("秒: {}", tmpdt.second());    // 15
        println!("纳秒: {}", tmpdt.nanosecond()); // 0
        println!("星期: {}", tmpdt.weekday()); // Sat
        let tmpt1 = naivedatetime_to_duration(tmpdt);
        println!("stamp: {}", tmpt1.as_millis());
    }
}

fn duration_to_naivedatetime(duration: std::time::Duration) -> chrono::NaiveDateTime {
    let secs = duration.as_secs() as i64;  // 注意转为 i64
    let nsecs = duration.subsec_nanos();
    chrono::NaiveDateTime::from_timestamp_opt(secs, nsecs).unwrap()
}

fn naivedatetime_to_duration(dt: chrono::NaiveDateTime) -> std::time::Duration {
    let secs = dt.and_utc().timestamp() as u64;  // 注意转为 u64
    let nanos = dt.and_utc().timestamp_subsec_nanos();
    std::time::Duration::new(secs, nanos)
}

fn type_str<T>(_val: &T) -> & 'static str {
    std::any::type_name::<T>()
}
